<template>
  <div class="edit-container">
    <el-form ref="formRef" :model="form" :rules="rules" size="default" label-width="100px" class="form-container">
      <!-- <el-form-item label="客户姓名" prop="customerId"> -->
      <!-- <el-select v-model="form.customerId" filterable clearable placeholder="请选择客户" style="width: 100%"> -->
      <!-- <el-option v-for="customer in customerList" :key="customer.customerId" -->
      <!-- :label="customer.customerName" :value="customer.customerId" /> -->
      <!-- </el-select> -->
      <!-- </el-form-item> -->

      <el-form-item label="客户姓名" prop="customerId">
        <el-select v-model="form.customerId" filterable clearable placeholder="请选择客户" style="width: 100%"
                   :loading="loading">
          <el-option v-for="customer in customerList" :key="customer.customerId"
                     :label="customer.customerName" :value="customer.customerId" />
          <template #empty>
            <el-empty description="暂无客户数据" v-if="!loading" />
          </template>
        </el-select>
      </el-form-item>


      <el-form-item label="客户喜好" prop="preferences">
        <el-input v-model="form.preferences" placeholder="请输入客户喜好" />
      </el-form-item>
      <el-form-item label="注意事项">
        <el-input v-model="form.attention" placeholder="请输入注意事项" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" placeholder="请输入备注信息" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, defineProps } from 'vue'
import { ElNotification } from 'element-plus'
import { saveOrUpdateInfo } from '@/api/customer/customerpreference/index.js'

// 接收父组件传递的props
const props = defineProps({
  closeEdit: {
    type: Function,
    required: true
  },
  customerList: {
    type: Function,
    required: true
  },
})

const loading = ref(false)

// 表单信息
const form = ref({
  customerId: '',
  preferences: '',
  attention: '',
  remark: ''
})

// 创建引用 用于表单必选项验证
const formRef = ref()
// 校验必选项规则
const rules = ref({
  customerId: [
    { required: true, message: '请选择客户', trigger: 'blur' }
  ],
  preferences: [
    { required: true, message: '请输入客户喜好', trigger: 'blur' }
  ]
})

// 保存按钮点击事件
const submitForm = () => {
  formRef.value.validate(valid => {
    if (valid) {
      saveOrUpdateInfo(form.value).then(() => {
        ElNotification({
          title: '成功',
          type: 'success',
          message: '添加成功'
        })
        props.closeEdit(true)
      }).catch(error => {
        ElNotification({
          title: '错误',
          type: 'error',
          message: error.message || '添加失败'
        })
      })
    }
  })
}
// 点击取消按钮
const cancel = () => {
  props.closeEdit(false)
}
</script>

<style scoped>
.edit-container {
  /* 水平居中对齐 */
  justify-content: center;
  /* 垂直居中对齐 */
  align-items: center;
  /* 高度占满父容器 */
  height: 100%;
}

.form-container {
  width: 400px;
  /* 如果允许换行，那么换行后，多行元素中的每一行也会垂直居中 */
  justify-content: center;
  /* 垂直居中对齐 */
  align-items: center;
}
</style>